#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QChart>
#include <QDebug>
#include <QValueAxis>
#include <QPolarChart>
#include <cmath>

// 测试镜像
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    timer = new QTimer(this);

    tick = 0;

    // 创建直角坐标系图表
    // QChart *chart = new QChart();
    // chart->setTitle("数据曲线 (直角坐标系)");

    // 创建极坐标系图表
    QPolarChart *chart = new QPolarChart();
    chart->setTitle("数据曲线 (极坐标系)");

    ui->graphicsView->setChart(chart);

    // QValueAxis *axisx = new QValueAxis();
    // axisx->setRange(0, 100); // 修改 x 轴范围
    // axisx->setLabelFormat("%d");
    // axisx->setTickCount(5);
    // axisx->setTitleText("时间 (s)"); // 修改 x 轴标题
    // axisx->setMinorGridLineVisible(false);

    // QValueAxis *axisy = new QValueAxis();
    // axisy->setRange(-2, 2); // 修改 y 轴范围
    // axisy->setLabelFormat("%d");
    // axisy->setTickCount(5);
    // axisy->setTitleText("振幅"); // 修改 y 轴标题
    // axisy->setMinorGridLineVisible(false);

    // chart->addAxis(axisx, Qt::AlignBottom);
    // chart->addAxis(axisy, Qt::AlignLeft);

    // 如果是极坐标系，使用角度轴和径向轴
    QValueAxis *axisAngle = new QValueAxis();
    axisAngle->setRange(0, 360); // 角度范围
    axisAngle->setLabelFormat("%d°");
    axisAngle->setTickCount(12);
    axisAngle->setTitleText("角度");
    chart->addAxis(axisAngle, QPolarChart::PolarOrientationAngular);

    QValueAxis *axisRadius = new QValueAxis();
    axisRadius->setRange(0, 2); // 半径范围
    axisRadius->setLabelFormat("%.1f");
    axisRadius->setTickCount(5);
    axisRadius->setTitleText("半径");
    chart->addAxis(axisRadius, QPolarChart::PolarOrientationRadial);

    seriesS = new QScatterSeries();
    seriesS->setMarkerSize(5);
    seriesL = new QLineSeries();
    seriesS->setName("坐标点");
    seriesL->setName("数据曲线");
    chart->addSeries(seriesS);
    chart->addSeries(seriesL);

    // 直角坐标系的轴绑定
    // seriesS->attachAxis(axisx);
    // seriesS->attachAxis(axisy);
    // seriesL->attachAxis(axisx);
    // seriesL->attachAxis(axisy);

    // 极坐标系的轴绑定
    seriesS->attachAxis(axisAngle);
    seriesS->attachAxis(axisRadius);
    seriesL->attachAxis(axisAngle);
    seriesL->attachAxis(axisRadius);

    connect(timer, &QTimer::timeout, this, &MainWindow::updateChart);
}

MainWindow::~MainWindow()
{
    delete ui;
}

// 横坐标不平移
void MainWindow::updateChart(void)
{
    int x = tick;
    double angle = x * 10 * M_PI / 180.0;
    double y = std::sin(angle);

    int x_axis = static_cast<int>(x) % 100;

    // 直角坐标系更新
    // seriesS->append(x_axis, y);
    // seriesL->append(x_axis, y);

    // 极坐标系更新
    double radius = std::abs(y); // 半径为 y 的绝对值
    double theta = x * 10;       // 角度
    if (theta >= 360)
        theta -= 360;
    seriesS->append(theta, radius);
    seriesL->append(theta, radius);

    tick += 1;

    if (tick % 100 == 0)
    {
        seriesS->clear();
        seriesL->clear();

        if (tick >= INT32_MAX / 2)
            tick = 0;
    }
}

void MainWindow::on_pushButton_start_clicked()
{
    qDebug("start");
    timer->start(33);
}

void MainWindow::on_pushButton_stop_clicked()
{
    qDebug("stop");
    timer->stop();
}

void MainWindow::on_pushButton_clear_clicked()
{
    qDebug("clear");
    seriesL->clear();
    seriesS->clear();
}
